//
//  RAPStatusCellToolBar.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/1/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPStatusCellToolBar: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        addSubview(repostButton) // 1
        addSubview(commentButton) // 2
        addSubview(attitudeButton) // 3
        addSubview(leftSplitImageView)
        addSubview(rightSplitImageView)
        
        
        installConstraints()
    }
    
    private func installConstraints() {
        repostButton.snp_makeConstraints { (make) -> Void in
            make.leading.height.bottom.equalTo(self)

        }
        
        commentButton.snp_makeConstraints { (make) -> Void in
            make.height.bottom.equalTo(self)
            make.leading.equalTo(repostButton.snp_trailing)
            make.width.equalTo(repostButton)
        }
        
        attitudeButton.snp_makeConstraints { (make) -> Void in
            make.height.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
            make.trailing.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        leftSplitImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.trailing.equalTo(repostButton)

        }
        
        rightSplitImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.leading.equalTo(attitudeButton)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusCellViewModel: RAPStatusCellViewModel? {
        didSet {
            if let statusModel = statusCellViewModel?.statusModel {
                repostButton.setTitle("转发\(statusModel.reposts_count)", forState: UIControlState.Normal)
                commentButton.setTitle("评论\(statusModel.comments_count)", forState: UIControlState.Normal)
                attitudeButton.setTitle("赞\(statusModel.attitudes_count)", forState: UIControlState.Normal)
            }
        }
    }
    
    // MARK: -  懒加载控件
    // 转发，评论，赞
    lazy var repostButton:  UIButton = {
       let repostButton = UIButton(title: "转发", fontSize: 12, imageName: "timeline_icon_retweet", backgroundImageName: "timeline_card_bottom_background", target: self, action: "repostButtonClicked", textColor: UIColor.lightGrayColor())
        
        return repostButton
    }()
    
    lazy var commentButton:  UIButton = {
        let commentButton = UIButton(title: "评论", fontSize: 12, imageName: "timeline_icon_comment", backgroundImageName: "timeline_card_bottom_background", target: self, action: "commentButtonClicked", textColor: UIColor.lightGrayColor())
        
        return commentButton
    }()
    
    lazy var attitudeButton:  UIButton = {
        let attitudeButton = UIButton(title: "赞", fontSize: 12, imageName: "timeline_icon_unlike", backgroundImageName: "timeline_card_bottom_background", target: self, action: "attitudeButtonClicked", textColor: UIColor.lightGrayColor())
        
        return attitudeButton
    }()
    
    lazy var leftSplitImageView: UIImageView = {
        let split = UIImageView(image: UIImage(named: "timeline_card_bottom_line"), highlightedImage: UIImage(named: "timeline_card_bottom_line_highlighted"))
        return split
    }()
    
    lazy var rightSplitImageView: UIImageView = {
        let split = UIImageView(image: UIImage(named: "timeline_card_bottom_line"), highlightedImage: UIImage(named: "timeline_card_bottom_line_highlighted"))
        return split
    }()
    
    @objc private func repostButtonClicked() {
        leftSplitImageView.highlighted = !leftSplitImageView.highlighted
        RAPLog("转发按钮点击")
    }
    
    @objc private func commentButtonClicked() {
        leftSplitImageView.highlighted = !leftSplitImageView.highlighted
        rightSplitImageView.highlighted = !rightSplitImageView.highlighted
        RAPLog("评论按钮点击")
    }
    
    @objc private func attitudeButtonClicked() {
        rightSplitImageView.highlighted = !rightSplitImageView.highlighted
        RAPLog("赞按钮点击")
    }

}
